package com.jackrain.nea.fc.cp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewCempQueryCmd;
import com.jackrain.nea.fc.cp.api.ViewCempQueryNoCmd;
import com.jackrain.nea.fc.cp.api.ViewHrEmpSaveCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * @author WSL
 * @create 2017/10/30
 */
@Api(description = "人员档案-查询")
@RestController
public class ViewCempCtrl {

    @Reference(version = "1.0", group = "cp")
    private ViewCempQueryCmd viewCempQueryCmd;

    @Reference(version = "1.0", group = "cp")
    private ViewCempQueryNoCmd viewCempQueryNoCmd;

    @Reference(version = "1.0", group = "cp")
    private ViewHrEmpSaveCmd viewHrEmpSaveCmd;


    @ApiOperation(value = "人员档案-非营业员查询")
    @RequestMapping(path = "/fc/api/emp/query", method = RequestMethod.POST)
    public JSONObject query(HttpServletRequest request,
                            @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.cp.api.ViewCempQueryCmd",
                    "cp", "1.0");
            result = ((ViewCempQueryCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }
        } catch (Exception e) {

            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }

    @ApiOperation(value = "人员档案-营业员查询")
    @RequestMapping(path = "/fc/api/emp/querytoapi", method = RequestMethod.POST)
    public JSONObject queryToApi(HttpServletRequest request,
                                 @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.cp.api.ViewCempQueryNoCmd",
                    "cp", "1.0");
            result = ((ViewCempQueryNoCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {

            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();
    }

    @ApiOperation(value = "HR与FC人员接口-02.人员档案")
    @RequestMapping(path = "/fc/api/hremp/save", method = RequestMethod.POST)
    public JSONObject hrEmpSave(HttpServletRequest request,
                                 @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.cp.api.ViewHrEmpSaveCmd",
                    "cp", "1.0");
            result = ((ViewHrEmpSaveCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {

            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
//                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();
    }
}
